package com.hrt.bigdata.mr.topn;

import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;

/**
 * 自定义reduce端的分组比较器
 * 相同年月的是一组，然后reduce获取的时候就获取到这一组了
 */
public class TGroupingComparator  extends WritableComparator {

    public  TGroupingComparator(){
        super(TKey.class,true);
    }

    @Override
    public int compare(WritableComparable a, WritableComparable b) {
        TKey k1 = (TKey)a;
        TKey k2 = (TKey)b;
        //  按着 年，月分组，
        int c1 = Integer.compare(k1.getYear(), k2.getYear());
        if(c1 == 0 ){
            return  Integer.compare(k1.getMonth(), k2.getMonth());
        }
        return c1;
    }
}
